/*  
 *  ------   [DM_06b] - Use of Expansion board     -------- 
 *  
 *  Explanation: This example shows how to receive packets through
 *  the Expansion board (SOCKET1). 
 *  
 *  Copyright (C) 2012 Libelium Comunicaciones Distribuidas S.L. 
 *  http://www.libelium.com 
 *  
 *  This program is free software: you can redistribute it and/or modify 
 *  it under the terms of the GNU General Public License as published by 
 *  the Free Software Foundation, either version 3 of the License, or 
 *  (at your option) any later version. 
 *  
 *  This program is distributed in the hope that it will be useful, 
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of 
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
 *  GNU General Public License for more details. 
 *  
 *  You should have received a copy of the GNU General Public License 
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>. 
 *  
 *  Version:           0.1 
 *  Design:            David Gascón 
 *  Implementation:    Yuri Carmona
 */


#include <WaspXBeeDM.h>

uint8_t network_address[2];

void setup()
{  
  // init USB port
  USB.ON();
  USB.println(F("DM_06b example"));

  // init XBee
  xbeeDM.ON(SOCKET1);
}

void loop()
{ 
  // check available data in RX buffer
  if( xbeeDM.available()>0 ) 
  {
    // read a packet when XBee has noticed it to us
    xbeeDM.treatData(); 
    
    // check RX flag after 'treatData'
    if( !xbeeDM.error_RX ) 
    {
      // read available packets
      while( xbeeDM.pos>0 )
      {
        // Available information in 'xbeeDM.packet_finished' structure
        // HERE it should be introduced the User's packet treatment        
        // For example: show DATA field:
             
        USB.print(F("Data: "));                    
        for(int i=0;i<xbeeDM.packet_finished[xbeeDM.pos-1]->data_length;i++)          
        { 
          // Print data payload
          USB.print(xbeeDM.packet_finished[xbeeDM.pos-1]->data[i],BYTE);          
        }
        USB.println("");

        //free memory
        free(xbeeDM.packet_finished[xbeeDM.pos-1]); 
        
        //free pointer
        xbeeDM.packet_finished[xbeeDM.pos-1]=NULL; 
        
        //Decrement the received packet counter
        xbeeDM.pos--; 
      }
    }
  }
} 
